package com.ruoyi.web.xxj.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.xxj.domain.ClassInfo;
import com.ruoyi.web.xxj.domain.Product;
import com.ruoyi.web.xxj.domain.ShopCart;
import com.ruoyi.web.xxj.mapper.ShopCartMapper;
import com.ruoyi.web.xxj.service.IClassInfoService;
import com.ruoyi.web.xxj.service.IProductService;
import com.ruoyi.web.xxj.service.IShopCartService;

import io.jsonwebtoken.lang.Collections;

/**
 * 购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-25
 */
@Service
public class ShopCartServiceImpl implements IShopCartService {

    @Autowired
    private ShopCartMapper shopCartMapper;
    
    @Autowired
    private IProductService productService;

    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public ShopCart selectShopCartById(Long id) {
        return shopCartMapper.selectShopCartById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param shopCart 购物车
     * @return 购物车
     */
    @Override
    public List<ShopCart> selectShopCartList(ShopCart shopCart) {
        return shopCartMapper.selectShopCartList(shopCart);
    }

    /**
     * 新增购物车
     *
     * @param shopCart 购物车
     * @return 结果
     */
    @Override
    public int insertShopCart(ShopCart shopCart) {
    	List<ShopCart> scs = shopCartMapper.selectShopCartList(new ShopCart(SecurityUtils.getWeChatUserId(),shopCart.getProductId()));
    	Product product = productService.selectProductById(shopCart.getProductId());
    	
    	if (CollectionUtils.isEmpty(scs)) {
    		shopCart.setPrice(new BigDecimal(shopCart.getQuantity()).multiply(product.getProductPrice()));
            shopCart.setWechatUserid(SecurityUtils.getLoginUser().getWechatUser().getId());
            shopCart.setCreateTime(DateUtils.getNowDate());
            return shopCartMapper.insertShopCart(shopCart);
		}else {
			ShopCart sc = scs.get(0);
			sc.setQuantity(sc.getQuantity()+shopCart.getQuantity());
			sc.setPrice(new BigDecimal(sc.getQuantity()).multiply(product.getProductPrice()));
			sc.setUpdateTime(DateUtils.getNowDate());
			return shopCartMapper.updateShopCart(sc);
		}
         
    }

    /**
     * 修改购物车
     *
     * @param shopCart 购物车
     * @return 结果
     */
    @Override
    public int updateShopCart(ShopCart shopCart) {
    	Product product = productService.selectProductById(shopCart.getProductId());
        shopCart.setPrice(new BigDecimal(shopCart.getQuantity()).multiply(product.getProductPrice()));
        shopCart.setUpdateTime(DateUtils.getNowDate());
        return shopCartMapper.updateShopCart(shopCart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartByIds(Long[] ids) {
        return shopCartMapper.deleteShopCartByIds(ids);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteShopCartById(Long id) {
        return shopCartMapper.deleteShopCartById(id);
    }

}
